package com.atomic.test.example.repository;

import com.atomic.test.example.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author dreamyao
 * @title
 * @date 2021/11/10 7:10 下午
 * @since 1.0.0
 */
public interface UserRepository extends JpaRepository<User, Integer> {

    User getUserByUserName(String userName);

    User getUserById(Integer id);

    User getUserByUserNameAndPassword(String userName, String password);

    @Modifying
    @Transactional
    @Query("update User set credit=:credit,lastVisit=:lastVisit where userName=:userName")
    void updateCredit(@Param("credit") Integer credit,
                      @Param("lastVisit") Date lastVisit,
                      @Param("userName") String userName);

    @Modifying
    @Transactional
    @Query("update User set password=:password where userName=:userName")
    void updatePassword(@Param("userName") String userName, @Param("password") String password);
}
